#include "Vertex.h"

namespace Render
{
	inline float rtVertex::operator[](const int index) const
	{
		if (index < 3)
		{
			return Position[index];
		}

		return TextureCoord[index];
	}

	inline float& rtVertex::operator[](const int index)
	{
		if (index < 3)
		{
			return Position[index];
		}

		return TextureCoord[index];
	}

	inline void rtVertex::lerp(const rtVertex& a, const rtVertex& b, const float f)
	{
		Position     = b.Position     * f + a.Position     * (1.0f - f);
		TextureCoord = b.TextureCoord * f + a.TextureCoord * (1.0f - f);
	}

	inline void rtVertex::lerpAll(const rtVertex& a, const rtVertex& b, const float f)
	{
		lerp(a, b, f);
		Normal      = b.Normal      * f + a.Normal      * (1.0f - f);
		Tangents[0] = b.Tangents[0] * f + a.Tangents[0] * (1.0f - f);
		Tangents[1] = b.Tangents[1] * f + a.Tangents[1] * (1.0f - f);
		Color       = b.Color       * f + a.Color       * (1.0f - f);
	}

	

	inline void rtVertex::setColor(unsigned int color)
	{
		Color = color;
	}

	inline unsigned int rtVertex::getColor()
	{
		return Color;
	}

	inline void rtVertex::clear()
	{
		Position.zero();
		TextureCoord.zero();
		Normal.zero();
		Tangents[0].zero();
		Tangents[1].zero();
		Color = 0;
	}

	void rtVertex::normalize()
	{
		Normal.normalize();

		Tangents[1] = Normal.cross(Tangents[0]);
		Tangents[1].normalize();

		Tangents[0] = Normal.cross(Tangents[1]);
		Tangents[0].normalize();
	}
}
